# -*- coding: utf-8 -*-
"""
Module Description: CD恢复
Date: 2019/1/7
Author: FQ
"""
from pyshare.utils.time_utils import getCT, getCountdown, get_another_time


class CDRecover(object):

    def __init__(self, value, refresh_time, cool_cd, max_value):
        self.value = value
        self.refresh_time = refresh_time
        self.cool_cd = cool_cd
        self.max_value = max_value
        self.left_sec = 0
        # 初始化时更新一次
        self.value, self.left_sec = self.left_value()

    def get_recover_value(self):
        """已恢复次数"""
        cover_sec = getCountdown(getCT(), self.refresh_time)
        add_value = int(cover_sec / self.cool_cd)
        over_sec = int(cover_sec % self.cool_cd)
        return add_value, self.cool_cd - over_sec

    def charge_at(self):
        """返回下一次补满的时间点"""
        return get_another_time(getCT(), seconds=self.left_sec)

    def left_value(self):
        if self.value < self.max_value:
            add_value, self.left_sec = self.get_recover_value()
            self.value = min(self.max_value, self.value + add_value)
            if self.value >= self.max_value:
                self.left_sec = 0
            if add_value > 0:
                self.refresh_time = get_another_time(getCT(), seconds=-(self.cool_cd - self.left_sec))

        return self.value, self.left_sec

    def add_value(self, add_v):
        """增加次数"""
        self.left_value()
        self.value += add_v
        if self.value >= self.max_value:
            self.left_sec = 0

    def use_vit(self):
        """使用体力"""
        if self.value >= self.max_value:
            self.refresh_time = getCT()

        self.value -= 1
        self.value, self.left_sec = self.left_value()
        return self.value, self.left_sec
